package com.wwwarehouse.usercenter.fragment.invite;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ApkTools;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.SendInviteResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 邀请注册
 * Created by Jackie on 2017/8/10.
 */

@Route(path = UserCenterConstant.PATH_INVITE_REGISTER_FRAGMENT)
public class InviteRegisterFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private ImageView mInviteWechatImg;
    private ImageView mInviteMsgImg;
    private ImageView mInviteF2fImg;
    private TextView mShowInvitedTxt;

    private String mAuthProcessId;
    private int mInviteType;

    private static final int REQUEST_SEND_INVITE = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_invite_register, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mInviteWechatImg = findView(mRootView, R.id.iv_invite_wechat);
        mInviteMsgImg = findView(mRootView, R.id.iv_invite_msg);
        mInviteF2fImg = findView(mRootView, R.id.iv_invite_f2f);
        mShowInvitedTxt = findView(mRootView, R.id.tv_show_invited);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mAuthProcessId = bundle.getString(UserCenterConstant.KEY_AUTH_PROCESS_UKID);
        }
    }

    private void initEvent() {
        mInviteWechatImg.setOnClickListener(this);
        mInviteMsgImg.setOnClickListener(this);
        mInviteF2fImg.setOnClickListener(this);
        mShowInvitedTxt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.iv_invite_wechat) {
            if (!ApkTools.isWeixinInstall(mActivity)) {
                toast(R.string.wechat_not_installed);
                return;
            }

            mInviteType = 1;
            sendInvite(mInviteType);
        } else if (id == R.id.iv_invite_msg) {
            mInviteType = 2;
            sendInvite(mInviteType);
        } else if (id == R.id.iv_invite_f2f) {
            mInviteType = 3;
            sendInvite(mInviteType);
        } else if (id == R.id.tv_show_invited) { //查看已邀请的人
            pushFragment(new ShowInvitedFragment());
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof InviteRegisterFragment) {
            mActivity.setTitle(R.string.invite_register);
        }
    }

    @Override
    public void requestDatas() {

    }

    private void sendInvite(int inviteType) {
        showProgressDialog(getString(R.string.waiting));

        Map map = new HashMap();

        if (!TextUtils.isEmpty(mAuthProcessId)) {
            //带权限邀请
            map.put("authProcessId", mAuthProcessId);
            map.put("authType", "30");
        } else {
            //普通邀请
            map.put("authType", "10");
        }

        map.put("inviteType", inviteType + "");

        NoHttpUtils.httpPost(UserCenterConstant.URL_SEND_INVITE, map, mOnResponseListener, REQUEST_SEND_INVITE);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_SEND_INVITE:
                    if ("0".equals(commonClass.getCode())) {
                        SendInviteResponseBean sendInviteResponseBean = JSON.parseObject(commonClass.getData().toString(), SendInviteResponseBean.class);
                        if (mInviteType == 1) {
                            Common.getInstance().shareWeixinFriend(mActivity, getString(R.string.invite_register), getString(R.string.invite_register_account), sendInviteResponseBean.getImageUrl(), sendInviteResponseBean.getInviteUrl());
                        } else if (mInviteType == 2) { //短信邀请
                            Uri uri = Uri.parse("smsto:");  //如果发送给指定的人: Uri uri = Uri.parse("smsto:10000");
                            Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                            intent.putExtra("sms_body", sendInviteResponseBean.getSmsContent());
                            startActivity(intent);
                        } else if (mInviteType == 3) { //面对面邀请
                            InviteRegisterF2FFragment inviteRegisterF2FFragment = new InviteRegisterF2FFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable(UserCenterConstant.KEY_REGISTER_SEND_INVITE, sendInviteResponseBean);
                            inviteRegisterF2FFragment.setArguments(bundle);
                            pushFragment(inviteRegisterF2FFragment);
                        }
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };
}
